package org.thss.petri.gef.commands;

import org.eclipse.gef.commands.Command;
import org.thss.petri.model.Arc;
import org.thss.petri.model.Node;
import org.thss.petri.model.PetriNet;

public class DeleteArcCommand extends Command {
	private Arc connection;
	private Node source;
	private Node target;
	private PetriNet net;

	public Arc getConnection() {
		return connection;
	}

	public void setConnection(Arc connection) {
		this.connection = connection;
		this.net = connection.getNet();
		this.source = connection.getSource();
		this.target = connection.getTarget();
	}

	@Override
	public void execute() {
		if (connection == null) {
			return;
		}
		connection.setSource(null);
		connection.setTarget(null);
		source.getOutgoings().remove(connection);
		target.getIncomings().remove(connection);
		net.getArcs().remove(connection);
		connection.setNet(null);
	}

	@Override
	public void undo() {
		if (connection == null) {
			return;
		}
		connection.setSource(source);
		connection.setTarget(target);
		source.getOutgoings().add(connection);
		target.getIncomings().add(connection);
		connection.setNet(net);
		net.getArcs().add(connection);
	}

	@Override
	public void redo() {
		execute();
	}
}
